package com.skyhigh.web.webservice.controllers;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.skyhigh.web.webservice.domain.Usuario;
import com.skyhigh.web.webservice.services.UsuarioService;

@Controller
public class LoginController {
	
	@Autowired
	private UsuarioService usuarioService;		
	protected final Log logger = LogFactory.getLog(getClass());	
		
		
	@RequestMapping(value="/login.htm", method = RequestMethod.POST)
	public String processSubmit(ModelMap model,
		@ModelAttribute("idUsuario") Integer idUsuario,
		@ModelAttribute("password") String password,
		BindingResult result, SessionStatus status) {				
 
		String now = (new Date()).toString();
        logger.info("Returning Login view with " + now);
                                
		Usuario usuario = new Usuario();
		usuario.setIdUsuario(idUsuario);
		usuario.setPassword(password);
		
		if(usuarioService.iniciarSesion(usuario)){
			status.setComplete();
			return "desktop";	
		}else{
			model.addAttribute("msgError", new String("ID Usuario o Password invalido, por favor intente nuevamente."));
			return "indexLoginFailed";			
		}                          
	}

}
